< previous page page_263 next page >

Page 263
The DoTheShellCall function performs the SHFileOperation call and is defined as follows:
Private Declare Sub lstrcpy Lib "kernel32" (dest As Any, src As Any)

' Note the alignment tricks here
Private Function DoTheShellCall(sh As SHFILEOPSTRUCT, Optional _
 SimpleCaption As String) As Long
   Dim titlebuf() As Byte
   Dim res&
   ReDim titlebuf(Len(SimpleCaption) + 1)
   Call lstrcpy(titlebuf(0), ByVal SimpleCaption)
   sh.lpszProgressTitle = VarPtr(titlebuf(0))
   RtlMoveMemory ByVal (VarPtr(sh.fFlags) + 2), ByVal _
   VarPtr(sh.fAnyOperationsAborted), 12
   res = SHFileOperation(sh)
   RtlMoveMemory ByVal VarPtr(sh.fAnyOperationsAborted), ByVal _
   (VarPtr(sh.fFlags) + 2), 12
   DoTheShellCall = res
End Function
The function allocates a byte array to hold the ANSI string that will contain the desired caption. The lstrcpy function copies the string into the byte buffer. The lstrcpy function in this case has both parameters defined As Any. The first parameter is passed as a byte, so the lstrcpy function sees a pointer to the first byte in the array. The second parameter is passed ByVal As String (notice that ByVal must be included explicitly in the function call because it is not included in the declaration). This causes the string to be passed as a NULL-terminated ANSI string. The lstrcpy function copies that string to the byte buffer.
The lpszProgressTitle field, which is now a Long variable, is loaded with the address of the first byte in this string. The RtlMoveMemory function then compresses the structure. The destination is the address of the padding, which is 2 bytes after the fFlags field, thus the address can be calculated as the address of the fFlags field plus 2. The source is the fAnyOperationsAborted field. A total of 12 bytes are copied. This achieves the movement shown in Figure S30-4.
The memory movement is reversed after the function is called so that you can read the data in the structure correctly, in case you need to examine changes to any of the fields.
That solves the problem of copying files, but, as you will soon see, we have only begun to look at the ShFileOperation function.

 
< previous page page_263 next page >